package com.yhyvr.api.service.store;/**
 * Created by zhangqin on 2017/6/23.
 */

import com.google.common.collect.Lists;
import com.lz.mybatis.jdbc.auto.dao.BoProxyMessageMapper;
import com.lz.mybatis.jdbc.auto.model.*;
import com.lz.tool.ListUtils;
import com.lz.tool.date.DateStyle;
import com.lz.tool.date.DateUtil;
import com.yhyvr.api.handle.AuthAnnotation;
import com.yhyvr.pojo.req.api.ApiReqBody;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.message.ApiMessageReq;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.store.message.ApiMessageResp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息服务
 *
 * @author
 * @create 2017-06-23 17:04
 **/
@Service
public class ApiMessageService {


    @Resource
    private BoProxyMessageMapper boProxyMessageMapper;

    @AuthAnnotation(iLogin = true)
    public ApiRespData<List<ApiMessageResp>> messageList(ApiReqData<ApiReqBody> apiReqBodyApiReqData){
        List<ApiMessageResp> list = Lists.newArrayList();
        BoProxyMessageExample boProxyMessageExample = new BoProxyMessageExample();
        boProxyMessageExample.createCriteria().andProxyIdEqualTo(apiReqBodyApiReqData.getCookieUserId());
        boProxyMessageExample.setOrderByClause("create_time desc");
        List<BoProxyMessage> boProxyMessages = boProxyMessageMapper.selectByExample(boProxyMessageExample);
        if(boProxyMessages!=null){
            for (BoProxyMessage boProxyMessage : boProxyMessages) {
                ApiMessageResp apiMessageResp = new ApiMessageResp();
                apiMessageResp.setMessageId(boProxyMessage.getId());
                apiMessageResp.setCreateTime(DateUtil.DateToString(boProxyMessage.getCreateTime(),DateStyle.YYYY_MM_DD_HH_MM_CN));
                apiMessageResp.setHasRead(boProxyMessage.getHasRead());
                apiMessageResp.setMessageContent(boProxyMessage.getMessageContent());
                apiMessageResp.setMessageTitle(boProxyMessage.getMessageTitle());
                apiMessageResp.setMessageType(boProxyMessage.getMessageType());
                list.add(apiMessageResp);
            }
        }
        return ApiRespData.buildSucc(list);
    }

    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiMessageResp> messageDetail(ApiReqData<ApiMessageReq> apiReqBodyApiReqData){
        BoProxyMessageExample boProxyMessageExample = new BoProxyMessageExample();
        boProxyMessageExample.createCriteria().andProxyIdEqualTo(apiReqBodyApiReqData.getCookieUserId()).andIdEqualTo(apiReqBodyApiReqData.getBody().getMessageId());
        boProxyMessageExample.setOrderByClause("create_time desc");
        BoProxyMessage boProxyMessage = ListUtils.getFirst(boProxyMessageMapper.selectByExample(boProxyMessageExample));
                ApiMessageResp apiMessageResp = new ApiMessageResp();
                apiMessageResp.setMessageId(boProxyMessage.getId());
                apiMessageResp.setCreateTime(DateUtil.DateToString(boProxyMessage.getCreateTime(),DateStyle.YYYY_MM_DD_HH_MM_CN));
                apiMessageResp.setHasRead(boProxyMessage.getHasRead());
                apiMessageResp.setMessageContent(boProxyMessage.getMessageContent());
                apiMessageResp.setMessageTitle(boProxyMessage.getMessageTitle());
                apiMessageResp.setMessageType(boProxyMessage.getMessageType());
        return ApiRespData.buildSucc(apiMessageResp);
    }
}
